<template>
  <div class="sys-pos-container">
    <el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
      <el-form :model="state.queryParams" ref="queryForm" :inline="true">
        <el-form-item :label="$t('message.pageConfiguration.positionName')">
          <el-input v-model="state.queryParams.name" :placeholder="$t('message.pageConfiguration.positionName')" clearable />
        </el-form-item>
        <el-form-item :label="$t('message.pageConfiguration.positionCode')">
          <el-input v-model="state.queryParams.code" :placeholder="$t('message.pageConfiguration.positionCode')" clearable />
        </el-form-item>
        <el-form-item>
          <el-button-group>
            <el-button type="primary" icon="ele-Search" @click="handleQuery" v-auth="'sysPos:list'"> {{ $t('message.pageConfiguration.query') }} </el-button>
            <el-button icon="ele-Refresh" @click="resetQuery"> {{ $t('message.pageConfiguration.reset') }} </el-button>
          </el-button-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="ele-Plus" @click="openAddPos" v-auth="'sysPos:add'"> {{ $t('message.pageConfiguration.add') }} </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="full-table" shadow="hover" style="margin-top: 8px">
      <el-table :data="state.posData" style="width: 100%" v-loading="state.loading" border>
        <el-table-column type="index" :label="$t('message.pageConfiguration.number')" width="55" align="center" />
        <el-table-column prop="name" :label="$t('message.pageConfiguration.positionName')" align="center" show-overflow-tooltip />
        <el-table-column prop="code" :label="$t('message.pageConfiguration.positionCode')" align="center" show-overflow-tooltip />
        <el-table-column prop="orderNo" :label="$t('message.pageConfiguration.sort')" width="70" align="center" show-overflow-tooltip />
        <el-table-column :label="$t('message.pageConfiguration.status')" width="70" align="center" show-overflow-tooltip>
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.status === 1">{{$t('message.pageConfiguration.enable')}}</el-tag>
            <el-tag type="danger" v-else>{{$t('message.pageConfiguration.disable')}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" :label="$t('message.pageConfiguration.createTime')" align="center" show-overflow-tooltip />
        <el-table-column prop="remark" :label="$t('message.pageConfiguration.remark1')" header-align="center" show-overflow-tooltip />
        <el-table-column :label="$t('message.pageConfiguration.operate')" width="140" fixed="right" align="center" show-overflow-tooltip>
          <template #default="scope">
            <el-button icon="ele-Edit" size="small" text type="primary" @click="openEditPos(scope.row)" v-auth="'sysPos:update'"> {{ $t('message.pageConfiguration.edit') }} </el-button>
            <el-button icon="ele-Delete" size="small" text type="danger" @click="delPos(scope.row)" v-auth="'sysPos:delete'"> {{ $t('message.pageConfiguration.del') }} </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <EditPos ref="editPosRef" :title="state.editPosTitle" @handleQuery="handleQuery" />
  </div>
</template>

<script lang="ts" setup name="sysPos">
import { onMounted, reactive, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import EditPos from '/@/views/system/pos/component/editPos.vue';

import { getAPI } from '/@/utils/axios-utils';
import { SysPosApi } from '/@/api-services/api';
import { SysPos } from '/@/api-services/models';
import { useI18n } from 'vue-i18n';

const { t } = useI18n();

const editPosRef = ref<InstanceType<typeof EditPos>>();
const state = reactive({
	loading: false,
	posData: [] as Array<SysPos>,
	queryParams: {
		name: undefined,
		code: undefined,
	},
	editPosTitle: '',
});

onMounted(async () => {
	handleQuery();
});

// 查询操作
const handleQuery = async () => {
	state.loading = true;
	var res = await getAPI(SysPosApi).apiSysPosListGet(state.queryParams.name, state.queryParams.code);
	state.posData = res.data.result ?? [];
	state.loading = false;
};

// 重置操作
const resetQuery = () => {
	state.queryParams.name = undefined;
	state.queryParams.code = undefined;
	handleQuery();
};

// 打开新增页面
const openAddPos = () => {
	state.editPosTitle = t('message.pageConfiguration.addPosition');
	editPosRef.value?.openDialog({ status: 1, orderNo: 100 });
};

// 打开编辑页面
const openEditPos = (row: any) => {
	state.editPosTitle = t('message.pageConfiguration.editPosition');
	editPosRef.value?.openDialog(row);
};

// 删除
const delPos = (row: any) => {
	ElMessageBox.confirm(t('message.pageConfiguration.confirmDelPosition')+`：【${row.name}】?`, t('message.messages.hint'), {
		confirmButtonText: t('message.pageConfiguration.confirm'),
		cancelButtonText: t('message.pageConfiguration.cancel'),
		type: 'warning',
	})
		.then(async () => {
			await getAPI(SysPosApi).apiSysPosDeletePost({ id: row.id });
			handleQuery();
			ElMessage.success(t('message.messages.deleteSuccess'));
		})
		.catch(() => {});
};
</script>
